package org.myspringframework.aop.framework;

import org.myspringframework.aop.AdvisedSupport;

//代理工厂
public class ProxyFactory {

    //配置属性类
    private AdvisedSupport advisedSupport;

    public ProxyFactory(AdvisedSupport advisedSupport) {
        this.advisedSupport = advisedSupport;
    }

    public Object getProxy() {
        return createAopProxy().getProxy();
    }

    private AopProxy createAopProxy() {
        //当没有实现接口或者设置了proxyTargetClass=true时，使用cglib代理
        if (advisedSupport.isProxyTargetClass()||advisedSupport.getTargetSource().getTargetClass().length==0) {
            //todo: cglib多重代理问题 生成重复的字节码 cglib Duplicate method name "newInstance" with signature
            return new Cglib2AopProxy(advisedSupport);
        }

        //todo: SpringBoot 2.x开始，AOP为了解决使用JDK动态代理可能导致的类型转换异常，而默认使用CGLIB（通过在配置文件设置spring.aop.proxy-target-class=true）。
        return new JdkDynamicAopProxy(advisedSupport);
        //return new Cglib2AopProxy(advisedSupport);
    }

}